/**
 * 重量信息实体对象
 * @constructor
 */
function WeightInfo(){
    // this.grossWeight = 0;   //毛重 kg
    // this.tareWeight = 0;   //皮重 kg
    // this.netWeight = 0;   //净重 kg

    this.u8_T = new Uint8Array(19);  //数据包缓存区
    this.u8_i = 0;   //数据包缓存区当前下标

    //解析出来的原始数据
    this.showWeight = 0;    //显示重量 kg
    this.showWeightType = 0;    //重量信息类型 0：毛重，1：净重
    this.digits = 1;    //小数点位置信息100，10，1，0.1，0.01，0.001
    this.showTareWeight = 0;    //解析出来的皮重信息
}

/**
 * 打印重量信息对象的属性值
 */
WeightInfo.prototype.getShowString = function () {
    return "显示重量:"+this.showWeight+"  小数点:"+this.digits +
        "  显示重量类型:"+this.showWeightType+
        "  皮重信息："+this.showTareWeight;
}
/**
 * 字节转换为8位二进制，数组按照位数排列，第0位~第7位，3 -> 1 1 0 0 0 0 0 0
 * @param byteData
 * @returns {any[]}
 */
WeightInfo.prototype.byteToBit = function(byteData){
    var a8 = byteData.toString(2);
    //补全二进制
    while (a8.length < 8){
        a8 = 0 + a8;
    }
    return [a8[7],a8[6],a8[5],a8[4],a8[3],a8[2],a8[1],a8[0]];
}

/**
 * 将6个字节的Uint8Array转换为6位数字
 * @param byte6Data
 * @returns {number}
 */
WeightInfo.prototype.byte6ToInt = function(byte6Data,startIndex,endIndex){
    var showWeightStr = "";
    for(var i = startIndex;i <= endIndex;i++){
        // console.log("byte6Data:"+byte6Data[i]);
        if(byte6Data[i] == 0x20){  //空格则不管
            // console.log("0x20  byte6Data:"+byte6Data[i]);
            continue;
        }
        var n = byte6Data[i] - 48;
        showWeightStr = showWeightStr + (n + "");
    }
    var showWeight = parseInt(showWeightStr);
    return showWeight;
}

/**
 * 解析重量信息数据包方法
 */
WeightInfo.prototype.parsingDataPackage = function(dataPackageBlob){
    // console.log(typeof dataPackageBlob);
    //如果是字符串则转为16进制
    if(typeof dataPackageBlob != "string"){
        return;
    }
    for(var i = 0,j = dataPackageBlob.length; i < j; i++){
        var str_a = dataPackageBlob.charCodeAt(i);
        if(str_a == 0x02){
            this.u8_T[0] = str_a;
            this.u8_i = 1;
            continue;
        }
        if(this.u8_i >= 18){ //如果数据超过限制
            this.u8_T[0] = 0x00;
            this.u8_i = 0;
        }
        if(this.u8_T[0] == 0x02){    //数据包起始符
            this.u8_T[this.u8_i] = str_a;
            this.u8_i ++ ;
            if(str_a == 0x0D){  //数据包结束符
                this.u8_i = 0;
                try {
                    this.parsingArrayBuffer(this.u8_T);
                }catch (e) {
                }
                this.u8_T[0] = 0x00; //起始符重置
            }
        }
    }
}

/**
 * 进一步解析重量数据包ArrayBuffer类型
 * @param dataPackage
 */
WeightInfo.prototype.parsingArrayBuffer = function(dataPackage){
    // console.log("dataPackage:"+dataPackage)
    if(dataPackage.byteLength === undefined || dataPackage.byteLength < 17){
        return;
    }
    var u8 = dataPackage;
    // console.log("u8:"+u8);
    if(u8.length < 17){
        // console.error("重量数据包长度不足！无法解析！");
        return;
    }
    //如果起始标识不是02，或者结束标志不是0D，则不做解析
    if(u8[0] != 0x02 || u8[16] != 0x0D){
        // console.error("为找到重量数据包起始标识！无法解析！");
        return;
    }
    // console.log("u8[0]:"+u8[0]);
    //状态字A
    var A = u8[1];
    // console.log("A:"+A);
    var bitA = this.byteToBit(A);
    // console.log("bitA:"+bitA);

    //小数位计算
    var digits_T = 1;
    if(bitA[0] == 0 && bitA[1] == 0 && bitA[2] == 0){
        digits_T = 100;
    }else if(bitA[1] == 0 && bitA[2] == 0 && bitA[0] == 1){
        digits_T = 10;
    }else if(bitA[0] == 0 && bitA[2] == 0 && bitA[1] == 1){
        digits_T = 1;
    }else if(bitA[0] == 1 && bitA[1] == 1 && bitA[2] == 0){
        digits_T = 0.1;
    }else if(bitA[0] == 0 && bitA[1] == 0 && bitA[2] == 1){
        digits_T = 0.01;
    }else if(bitA[0] == 1 && bitA[2] == 1 && bitA[1] == 0){
        digits_T = 0.001;
    }
    this.digits = digits_T;

    //状态字B
    var B = u8[2];
    // console.log("B:"+B);
    var bitB = this.byteToBit(B);
    // console.log("bitB:"+bitB);
    this.showWeightType = bitB[0];   //重量信息类型 0：毛重，1：净重

    //状态字C，暂时不做解析，因为该状态字没有可用信息

    //显示重量 下标4~9字节
    this.showWeight = this.byte6ToInt(dataPackage,4,9);

    //皮重
    this.showTareWeight = this.byte6ToInt(dataPackage,10,15);
    // this.showTareWeight = this.tareWeight;
    // this.tareWeight *= this.digits;  //皮重也加上小数点？
    // console.log("tareWeight:"+this.tareWeight);

    // if(this.showWeightType == 1){
    //     this.netWeight = showWeight;
    //     this.grossWeight = this.netWeight + this.tareWeight;
    // }else if(this.showWeightType == 0){
    //     this.grossWeight = showWeight;
    //     this.netWeight = this.grossWeight - this.tareWeight;
    // }
    // }
}

/**
 * 解析模板打印信息方法
 */
WeightInfo.prototype.parsingPrintInfo = function (printStr) {
    //解析打印字符，设置重量
    var printLine = printStr.split(/[\r\n]/,11);
    if(printLine.length < 9){
        return;
    }
    alert(printLine);
    var mzStr = printLine[6];
    var pzStr = printLine[7];
    var jzStr = printLine[8];
    this.grossWeight = mzStr.replace(/[^0-9]/ig,"");
    this.tareWeight = pzStr.replace(/[^0-9]/ig,"");
    this.netWeight = jzStr.replace(/[^0-9]/ig,"");

    // "METTER TOLEDO SCALE WEIGHING REPORT 08:00 2011/10/28\n" +
    // "CN:0023\n" +
    // "ID:SUD-A1234\n" +
    // "Cargo:CPPER TUBE\n" +
    // "Client:Changzhou PORT\n" +
    // "Gross Weight:36000 kg\n" +
    // "Tare Weight:12000kg\n" +
    // "Net Weight:24000kg";

    // this.grossWeight = 36000;
    // this.tareWeight = 12000;
    // this.netWeight = 24000;
}